from bs4 import BeautifulSoup
import urllib.request
import urllib
import re
import pymysql.cursors
import flask
from flask import request

def getData_start(self,i):
    #获取数据库游标
    connect = pymysql.Connect(self.host,self.user,self.passwd,self.db)
    #获取数据库游标
    cursor = connect.cursor()
    url = "http://www.bibizyz8.com/index-%d.html" % (i)
    response = urllib.request.urlopen(url)
    # 获取最初整个页面
    content = response.read().decode('utf-8')
    # 定义正则表达式
    reg_img =re.compile(r'<a.*?href="(/.+?\/.+?\/)".*?>(.*?)</a>')
    # 匹配正则表达式获取数据
    imglist=reg_img.findall(content)
    for img in imglist:
        urltext=urllib.request.urlopen("http://www.bibizyz8.com"+img[0])
        urlavi=urltext.read().decode('utf-8')
        soup = BeautifulSoup(urlavi, 'html.parser')
        p=soup.find_all('input')
        m=soup.find_all('img')
        urlavi=p[2]['value']
        urlimg=m[1]['src']
        # SQL 插入语句
        StringSql = "INSERT INTO %s(url,name, urlavi, urlimg)VALUES ('%s', '%s', '%s', '%s')" % (self.table,urltext.url, img[1], urlavi, urlimg)
        try:
            # 执行SQL
            cursor.execute(StringSql)
            # 提交到数据库执行
            connect.commit()
            print(urltext.url+'  '+img[1]+'  '+urlavi+'  '+urlimg)
        except:
            # 如果发生错误则回滚
            connect.rollback()
            print("写入失败")
        # 关闭数据库连接
    connect.close()